package com.carlinfo.stu_ques.question.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.util.DateUtil;
import com.carlinfo.stu_ques.question.dao.IAnswerDao;
import com.carlinfo.stu_ques.question.dao.IQuestionDao;
import com.carlinfo.stu_ques.question.dao.impl.answerDaoImpl;
import com.carlinfo.stu_ques.question.dao.impl.questionDaoImpl;
import com.carlinfo.stu_ques.question.pojo.Answer;
import com.carlinfo.stu_ques.question.pojo.Question;
import com.carlinfo.stu_ques.question.service.IQueAnsService;
import com.carlinfo.stu_ques.users.pojo.Level;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.IUsersService;
import com.carlinfo.stu_ques.users.service.impl.UsersServiceImpl;

public class QueAnsServiceImpl implements IQueAnsService
{
	/* 答案的操作 */
	/**
	 * 答案的CRUD
	 */
	private IUsersService usersService;
	private IAnswerDao answerDao = new answerDaoImpl();
	private DateUtil dateUtil = new DateUtil();

	@Override
	public int saveOneAnswerService(Answer answer)
	{
		int res = this.answerDao.saveOneDao(answer);
		/**
		 * 存储答案的时候，加上问题的关联关系
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", answer.getQuestionId());
		Question question = questionDao.selectOneDao(condMap);
		List<Answer> selectList = this.answerDao.selectListDao();
		int count = 0;
		for (Answer answer2 : selectList)
		{
			if (answer.getQuestionId() == answer2.getQuestionId())
			{
				count++;
			}
		}
		/**
		 * 将人数给问题
		 */
		question.setPeople(count);
		/**
		 * 对学生的pojo进行操作
		 */
		condMap.clear();
		condMap.put("id", answer.getStuId());
		Students students = this.usersService.selectOneStudentService(condMap);
		/**
		 * 判断时间是否是昨天，如果是，就先将昨天的清零
		 */
		if(dateUtil.isYesterday(new Date(),students.getUpdateTime())) {
			students.setTodayAccuracy(0);
			students.setTodayTrueCount(0);
			students.setTodayFalseCount(0);
			students.setTodayCount(0);
			System.out.println("我将昨天的清零了");
		}
		students.setTotalCount(students.getTotalCount() + 1);
		students.setTodayCount(students.getTodayCount()+1);
		if (answer.isShiFou())
		{
			students.setScore(students.getScore()+question.getScore());
			students.setTodayTrueCount(students.getTodayTrueCount()+1);
			students.setCountTrue(students.getCountTrue() + 1);
		} else
		{
			students.setTodayFalseCount(students.getTodayFalseCount()+1);
			students.setCountFalse(students.getCountFalse()+1);
		}
		/**
		 * 正确率
		 */
		students.setAccuracy(Double.valueOf(students.getCountTrue())/students.getTotalCount());
		students.setTodayAccuracy(Double.valueOf(students.getTodayTrueCount())/students.getTodayCount());
		students.setUpdateTime(new Date());
		this.usersService.updateOneStudentService(students);
		return res;
	}

	@Override
	public void updateOneAnswerService(Answer answer)
	{
		this.answerDao.updateOneDao(answer);
	}

	@Override
	public void deleteOneAnswerService(Answer answer)
	{
		this.answerDao.deleteOneDao(answer);
	}

	@Override
	public List<Answer> selectListAnswerService()
	{
		List<Answer> selectList = this.answerDao.selectListDao();
		/**
		 * 依次将学生和问题的关联关系加进去
		 */
		for (Answer answer : selectList)
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("id", answer.getQuestionId());
			Question question = this.questionDao.selectOneDao(condMap);
			answer.setQuestion(question);
			condMap.put("id", answer.getStuId());
			Students students =this.usersService.selectOneStudentService(condMap);
			answer.setStudents(students);
		}
		return selectList;
	}

	@Override
	public Answer selectOneAnswerService(Map<String, Object> condMap)
	{
		return this.answerDao.selectOneDao(condMap);
	}

	/* 问题的操作 */
	/**
	 * 问题的CRUD
	 */
	private IQuestionDao questionDao = new questionDaoImpl();

	@Override
	public int saveOneQuestionService(Question question)
	{
		return this.questionDao.saveOneDao(question);
	}

	@Override
	public void updateOneQuestionService(Question question)
	{
		this.questionDao.updateOneDao(question);
	}

	@Override
	public void deleteOneQuestionService(Question question)
	{
		this.questionDao.deleteOneDao(question);
	}

	@Override
	public List<Question> selectListQuestionService()
	{
		return this.questionDao.selectListDao();
	}

	@Override
	public Question selectOneQuestionService(Map<String, Object> condMap)
	{
		return this.questionDao.selectOneDao(condMap);
	}

	@Override
	public String printAnswerTiltle()
	{
		return "id\t问题\t答案\t学生\t对错\t\t创建时间\t\t\t更新时间";
	}

	@Override
	public String printQuestionTiltle()
	{
		return "id\t问题\t分数\t人数\t创建时间\t\t\t更新时间";
	}

	public IUsersService getUsersService()
	{
		return usersService;
	}

	public void setUsersService(IUsersService usersService)
	{
		this.usersService = usersService;
	}

	@Override
	public boolean questionDbTofile(String path)
	{
		return this.questionDao.dbToFile(path);
	}

	@Override
	public boolean questionFileToDb()
	{
		return this.questionDao.fileToDb();
	}

	@Override
	public boolean answerDbTofile(String path)
	{
		return this.answerDao.dbToFile(path);
	}

	@Override
	public boolean answerFileToDb()
	{
		return this.answerDao.fileToDb();
	}

}
